package br.com.expositor.apresentacao.infra.imp;

import org.apache.wicket.Component;
import org.apache.wicket.RestartResponseAtInterceptPageException;
import org.apache.wicket.authorization.Action;
import org.apache.wicket.authorization.IAuthorizationStrategy;
import org.apache.wicket.authorization.IUnauthorizedComponentInstantiationListener;

import br.com.expositor.apresentacao.infra.session.SessionExpositor;
import br.com.expositor.apresentacao.login.PaginaLogin;

public class ExpositorAuthorizationStrategy implements
		IUnauthorizedComponentInstantiationListener, IAuthorizationStrategy {

	private static ExpositorAuthorizationStrategy instance;

	/**
	 * @return
	 */
	public static ExpositorAuthorizationStrategy getInstance() {
		if (instance == null) {
			instance = new ExpositorAuthorizationStrategy();
		}
		return instance;
	}

	/**
	 * Singleton
	 */
	private ExpositorAuthorizationStrategy() {
	}

	@Override
	public boolean isActionAuthorized(final Component component,
			final Action action) {
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public boolean isInstantiationAuthorized(final Class componentClass) {
		if (PaginaTemplate.class.isAssignableFrom(componentClass)) {
			return SessionExpositor.get().isAuthenticated();
		} else {
			return true;
		}
	}

	/**
	 * @see org.apache.wicket.authorization.IUnauthorizedComponentInstantiationListener#onUnauthorizedInstantiation(org.apache.wicket.Component)
	 */
	@Override
	public void onUnauthorizedInstantiation(final Component component) {
		throw new RestartResponseAtInterceptPageException(PaginaLogin.class);
	}
}